package com.ymatou.iapi.thirdpartysync.testcase;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.util.HSSFColor.LIGHT_ORANGE;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.thirdpartysync.parameter.GetPromotionInfoBean;
import com.ymatou.iapi.thirdpartysync.service.GetPromotionInfoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomShop2cnCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.ThirdpartysyncCallService;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.model.livesellerShop2cn.LiveRoom;
import com.ymttest.database.model.thirdpartysync.promoterproduct;
import com.ymttest.database.model.thirdpartysync.promoterproductcommission;
import com.ymttest.database.model.thirdpartysync.promotionproduct;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.database.sqlwapper.thirdpartysync.DefThirdpartysyncWapper;
import com.ymttest.database.sqlwapper.thirdpartysync.thirdpartysyncWapper;
import com.ymttest.utils.DesUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*根据推广id获取Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetPromotionInfo{
	private static GetPromotionInfoBean getpromotioninfoBean;
	private static GetPromotionInfoCall getpromotioninfoCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据推广id获取Test");
	}

	@Before
	public void caseUp() {
		getpromotioninfoBean=new GetPromotionInfoBean();
		getpromotioninfoCall=new GetPromotionInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getpromotioninfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionInfo_001() {
		Logger.start(true,"根据PromotionId查询，成功");
		try {
			
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV3.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(500);
			//添加推广商品
			ThirdpartysyncCallService.insertPromotionProduct(productId, sellerId, 0.03);
			Thread.sleep(2000);
			ThirdpartysyncCallService.GetDouyinProductInfo(productId);
			promoterproduct promoterproduct = new DefThirdpartysyncWapper().selectPromoterProduct(productId).get(0);
			
			getpromotioninfoBean.setPromotionId(promoterproduct.getTrackId());
			getpromotioninfoCall.setData(getpromotioninfoBean);
			getpromotioninfoCall.callService();
			Logger.verifyEquals(true, getpromotioninfoCall.getJsonObject().get("success").getAsBoolean(), "验证success");
			JsonObject data = getpromotioninfoCall.getJsonObject().get("data").getAsJsonObject();
			Logger.verifyEquals(1, data.get("promoterId").getAsInt(), "验证promoterId");
			Logger.verifyEquals(1, data.get("platformId").getAsInt(), "验证platformId");
			Logger.verifyEquals("pidabcd", data.get("pid").getAsString(), "验证pid");
			Logger.verifyEquals(0.03, data.get("commission").getAsDouble(), "验证commission");
			Logger.verifyEquals(productId, data.get("productId").getAsString(), "验证productId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionInfo_002() {
		Logger.start(true,"PromotionId为空，失败");
		try {
//			getpromotioninfoBean.setPromotionId("96c7b598-fdba-4172-a99c-6d162ce36792");
			getpromotioninfoCall.setData(getpromotioninfoBean);
			getpromotioninfoCall.callService();
			
			Logger.verifyEquals(false, getpromotioninfoCall.getJsonObject().get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals("promotionId:promotionId不能为空", getpromotioninfoCall.getString("errorMessage"), "验证错误提示");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionInfo_003() {
		Logger.start(true,"PromotionId错误，失败");
		try {
			getpromotioninfoBean.setPromotionId("12312313-fdba-4411-3312-6d162ce36792");
			getpromotioninfoCall.setData(getpromotioninfoBean);
			getpromotioninfoCall.callService();
			
			Logger.verifyEquals(false, getpromotioninfoCall.getJsonObject().get("success").getAsBoolean(), "验证success");
			Logger.verifyIsContains("达人联盟商品不存在", getpromotioninfoCall.getString("errorMessage"), "验证错误提示");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	private void verifycomm(JsonObject data) throws Exception {
		Logger.comment("数据库验证");
		
		String productId = data.get("product_id").getAsString();
		promotionproduct promotion = new DefThirdpartysyncWapper().selectPromotionproduct(productId).get(0);
		 promoterproductcommission ppc = new DefThirdpartysyncWapper().selectPromoterProductCommission(productId).get(0);	
		YmtProductsI product = new YmtProductsIWapper().selectProductByProductId(productId).get(0);
		Logger.verifyEquals(product.getsPicUrl(), data.get("cover").getAsString(),"验证首图");
		Logger.verifyEquals(product.getsPicUrl(), data.get("images").getAsString().split(",")[0],"验证全部图片");
		Logger.verifyEquals(product.getiStockNum(), data.get("stock_num").getAsInt(),"验证stock_num");
		Logger.verifyEquals(product.getsProduct(), data.get("title").getAsString(),"验证title");
//		Logger.verifyEquals(product.getsProduct(), data.get("market_price").getAsString(),"验证market_price");
//		Logger.verifyEquals(product.getsProduct(), data.get("price").getAsString(),"验证price");
		Logger.verifyEquals(ppc.getCommission().doubleValue()*100, data.get("cos_ratio").getAsDouble(),"验证cos_ratio");
		Logger.verifyEquals(product.getSellerLoginId(), data.get("shop_name").getAsString(),"验证shop_name");
		Logger.verifyEquals(product.getiUserId(), data.get("shop_id").getAsInt(),"验证shop_id");
		Logger.verifyEquals(true, data.get("is_self").getAsBoolean(),"验证is_self");
		
	}
	
	
	private void verify(JsonObject data) throws Exception {
		Logger.comment("数据库验证");
		
		String productId = data.get("product_id").getAsString();
		promotionproduct promotion = new DefThirdpartysyncWapper().selectPromotionproduct(productId).get(0);
		
		YmtProductsI product = new YmtProductsIWapper().selectProductByProductId(productId).get(0);
		System.out.println(product);
		
		Logger.verifyEquals(product.getsPicUrl(), data.get("cover").getAsString(),"验证首图");
		Logger.verifyEquals(product.getsPicUrl(), data.get("images").getAsString().split(",")[0],"验证全部图片");
		Logger.verifyEquals(product.getiStockNum(), data.get("stock_num").getAsInt(),"验证stock_num");
		Logger.verifyEquals(product.getsProduct(), data.get("title").getAsString(),"验证title");
//		Logger.verifyEquals(product.getsProduct(), data.get("market_price").getAsString(),"验证market_price");
//		Logger.verifyEquals(product.getsProduct(), data.get("price").getAsString(),"验证price");
		Logger.verifyEquals(promotion.getCommission().doubleValue()*100, data.get("cos_ratio").getAsDouble(),"验证cos_ratio");
		Logger.verifyEquals(product.getSellerLoginId(), data.get("shop_name").getAsString(),"验证shop_name");
		Logger.verifyEquals(product.getiUserId(), data.get("shop_id").getAsInt(),"验证shop_id");
		Logger.verifyEquals(true, data.get("is_self").getAsBoolean(),"验证is_self");
		
	}
	
}